package su.samcom.calc.controller.validator;

import org.apache.commons.lang.StringUtils;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import su.samcom.calc.controller.form.CalculatorForm;
import su.samcom.calc.model.entity.Location;

/**
 * Created by Владислав on 10.01.14.
 */
public class CalculatorFormValidator {

    public final static String LOCATION_FROM_ERROR = "locationFromError";
    public final static String LOCATION_TO_ERROR = "locationToError";
    public final static String CBM_ERROR = "cbmError";
    public final static String LENGTH_ERROR = "lengthError";
    public final static String WEIGHT_ERROR = "weightError";
    public final static String COMMON_ERROR = "commonError";
    public final static String CARGO_TYPE_ERROR = "cargoTypeError";

    public static boolean validateCargoParameters(CalculatorForm calculatorForm, BindingResult bindingResult, Model model) {
        boolean valid = true;
        if(calculatorForm.getCargoType() == null){
            model.addAttribute(CARGO_TYPE_ERROR, "Выберите тип груза");
            valid = false;
        }
        if(calculatorForm.getCbm() == null){
            model.addAttribute(CBM_ERROR, "Заполните поле");
            valid = false;
        }
        if(calculatorForm.getLength() == null){
            model.addAttribute(LENGTH_ERROR,"Заполните поле");
            valid = false;
        }
        if(calculatorForm.getWeight() == null){
            model.addAttribute(WEIGHT_ERROR,"Заполните поле");
            valid = false;
        }
        if(bindingResult.hasFieldErrors("cbm")){
            model.addAttribute(CBM_ERROR,"Значение не корректно");
            valid = false;
        }
        if(bindingResult.hasFieldErrors("length")){
            model.addAttribute(LENGTH_ERROR,"Значение не корректно");
            valid = false;
        }
        if(bindingResult.hasFieldErrors("weight")){
            model.addAttribute(WEIGHT_ERROR,"Значение не корректно");
            valid = false;
        }
        return valid;
    }

    public static boolean validateLocationFrom(CalculatorForm calculatorForm, Model model) {
        if(calculatorForm.getCountryFromId() == null || StringUtils.isBlank(calculatorForm.getLocationFrom()) ){
            model.addAttribute(LOCATION_FROM_ERROR,"Введите пункт отправки");
            return false;
        }
        return true;
    }

    public static boolean validateLocationTo(CalculatorForm calculatorForm, Model model) {
        if(calculatorForm.getCountryToId() == null || StringUtils.isBlank(calculatorForm.getLocationTo()) ){
            model.addAttribute(LOCATION_TO_ERROR,"Введите пункт назначения");
            return false;
        }
        return true;
    }

    public static boolean validateLocation(Location locationTo, Model model) {
        if(locationTo == null){
            model.addAttribute(CalculatorFormValidator.LOCATION_TO_ERROR, "Пункт назначения не найден");
            return false;
        }
        return true;
    }

    public static boolean validateLocationFrom(Location locationFrom, Model model) {
        if(locationFrom == null){
            model.addAttribute(CalculatorFormValidator.LOCATION_FROM_ERROR, "Пункт отправки не найден");
            return false;
        }
        return true;
    }
}
